﻿using Inovout.Data.Domain;
using System.Linq;

namespace Inovout.Data.Repository
{
    public static class PagedQueryableExtensions
    {
        public static IDataObjectPagedSet<T> ToPagedSet<T>(this IQueryable<T> query, int pageSize, int? pageIndex)
        {
            pageIndex = pageIndex.HasValue ? pageIndex.Value : 1;
            int resultCount = query.Count();
            query = query.Skip((pageIndex.Value - 1) * pageSize).Take(pageSize);
            return new DataObjectPagedSet<T>(query, resultCount,
                pageSize, pageIndex.Value);
        }
    }

}
